﻿Imports System.IO

Partial Class Paginas_MantenerEventos
    Inherits System.Web.UI.Page

#Region "Funciones y Procedimientos Internos"
    Private Sub CargarArchivo()
        Dim miFichero As HttpPostedFile = Me.FileUpload1.postedfile
        Dim kb As Double = miFichero.ContentLength / 1024
        Dim nombreFichero As String = miFichero.FileName
        Dim mimeType As String = miFichero.ContentType.Trim
        Dim stream As FileStream = miFichero.InputStream
        miFichero.SaveAs(Server.MapPath("~/Paginas/img/Paso"))
    End Sub

    Private Sub CargarCombos()
        Call CargarCombosHora()
        Call UtilidadesPagina.CargarDropSINO(Me.ddlImpresion, "NO")

        Dim miCollect As New Collection
        Dim miNegTAdm As New dllNegocio.NegTiposAsistente(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollect = miNegTAdm.ListaTipoAsistenteVigente()
            Me.ddlTipoAsistencia.DataSource = miCollect
            Me.ddlTipoAsistencia.DataTextField = "Descripcion"
            Me.ddlTipoAsistencia.DataValueField = "Codigo"
            Me.ddlTipoAsistencia.DataBind()
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegTAdm.Dispose()
            miNegTAdm = Nothing
        End Try

        Dim miCollectI As New Collection
        Dim miNegImgAdm As New dllNegocio.NegImagenes(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollectI = miNegImgAdm.ListaImagenesVigente()
            Me.ddlImagen.DataSource = miCollectI
            Me.ddlImagen.DataTextField = "Descripcion"
            Me.ddlImagen.DataValueField = "Codigo"
            Me.ddlImagen.DataBind()
        Catch ex As Exception
        Finally
            miCollectI = Nothing
            miNegImgAdm.Dispose()
            miNegImgAdm = Nothing
        End Try

        Dim miNegCEdm As New dllNegocio.NegCategoriaEventos(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollect = miNegCEdm.ListaCategoriasEventoActivas()
            Me.ddlCategoria.DataSource = miCollect
            Me.ddlCategoria.DataTextField = "Descripcion"
            Me.ddlCategoria.DataValueField = "Codigo"
            Me.ddlCategoria.DataBind()
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegCEdm.Dispose()
            miNegCEdm = Nothing
        End Try

        Dim miNegLEdm As New dllNegocio.NegLugaresEventos(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollect = miNegLEdm.ListaLugaresEventoActivas()
            Me.ddlLugarEvento.DataSource = miCollect
            Me.ddlLugarEvento.DataTextField = "Descripcion"
            Me.ddlLugarEvento.DataValueField = "Codigo"
            Me.ddlLugarEvento.DataBind()
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegLEdm.Dispose()
            miNegLEdm = Nothing
        End Try
    End Sub

    Private Sub CargarCombosHora()
        Dim HoraIni As DateTime = "2010/01/01 00:00:01"
        For x As Integer = 1 To (24 * 2)
            Me.ddlHorasIni.Items.Add(New ListItem(HoraIni.ToString("HH:mm"), CInt(HoraIni.ToString("HHmm"))))
            Me.ddlHorasFin.Items.Add(New ListItem(HoraIni.ToString("HH:mm"), CInt(HoraIni.ToString("HHmm"))))
            HoraIni = DateAdd(DateInterval.Minute, 30, HoraIni)
        Next
    End Sub

    Private Sub Inicializar()
        Me.btnAgregarFiltro.Visible = False
        Me.chkVigente.Visible = False
        Call CargarCombos()
    End Sub

    Private Sub CargarEvento()
        Dim miCollect As New Collection
        Dim miNegEven As New dllNegocio.NegEventos(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollect = miNegEven.ListarEventos(Me.hdnIdEvento.Value.Trim, "", 0)
            If miCollect.Count > 0 Then
                Dim objEven As dllEntidades.clsEventos
                objEven = CType(miCollect.Item(1), dllEntidades.clsEventos)
                With objEven
                    Me.chkVigente.Checked = IIf(.PublicacionEventoActivo = "SI", True, False)
                    Me.hdnIdEvento.Value = .Codigo
                    Me.ddlImagen.SelectedValue = .CodigoImagen
                    Me.ddlTipoAsistencia.SelectedValue = .TipoAsistente
                    Me.txtDescripcion.Text = .DescripcionEvento
                    Me.txtFecIni.Text = .FechaHoraInicioPublicacion.ToString("dd/MM/yyyy")
                    Me.ddlHorasIni.SelectedValue = .FechaHoraInicioPublicacion.ToString("HHmm")
                    Me.txtFechaFin.Text = .FechaHoraFinPublicacion.ToString("dd/MM/yyyy")
                    Me.ddlHorasFin.SelectedValue = .FechaHoraFinPublicacion.ToString("HHmm")
                    Me.hdnImagen.Value = .ImagenMailEvento
                    Me.ddlImpresion.SelectedValue = .ImprimeInvitacion
                    Me.txtNombre.Text = .Nombre
                    Me.txtEntDiponibles.Text = .EntradasDisponibles
                End With
                Me.btnAgregarFiltro.Visible = True
            End If
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegEven.Dispose()
            miNegEven = Nothing
        End Try
    End Sub
#End Region

#Region "EVENTOS DE LA PAGINA"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack() Then
            Call Inicializar()
            If (Request.QueryString("EventoID")) <> "" Then
                If Request.QueryString("EventoID") <> "0" Then
                    Me.hdnIdEvento.Value = Request.QueryString("EventoID")
                    Call CargarEvento()
                Else
                    Me.hdnIdEvento.Value = "0"
                End If
            Else
                Me.hdnIdEvento.Value = "0"
            End If
        End If
    End Sub
#End Region

    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
        Dim objNeg As New dllNegocio.NegEventos(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim objEvento As New dllEntidades.clsEventos
        Try
            With objEvento
                .PublicacionEventoActivo = IIf(Me.chkVigente.Checked = True, "SI", "NO")
                .Codigo = Me.hdnIdEvento.Value
                .CodigoImagen = Me.ddlImagen.SelectedItem.Value
                .TipoAsistente = Me.ddlTipoAsistencia.SelectedItem.Value
                .DescripcionEvento = Me.txtDescripcion.Text.Trim
                .FechaHoraFinPublicacion = UtilidadesPagina.CambiarFormatoFecha(Me.txtFechaFin.Text) & " " & Me.ddlHorasFin.SelectedItem.Text.Trim & ":00"
                .FechaHoraInicioPublicacion = UtilidadesPagina.CambiarFormatoFecha(Me.txtFecIni.Text) & " " & Me.ddlHorasIni.SelectedItem.Text.Trim & ":00"
                .ImprimeInvitacion = Me.ddlImpresion.SelectedItem.Text
                .Nombre = Me.txtNombre.Text
                .CodigoLugar = Me.ddlLugarEvento.SelectedItem.Value
                .CodigoCategoriaEvento = Me.ddlCategoria.SelectedItem.Value
                .CodigoTipoAsistente = Me.ddlTipoAsistencia.SelectedItem.Value
                .EntradasDisponibles = Me.txtEntDiponibles.Text
                .ImagenMailEvento = ""
            End With
            Dim ResulGrabar As Integer

            If Me.hdnIdEvento.Value = 0 Then
                ResulGrabar = objNeg.GrabarEvento(objEvento)
                Dim nombreImagen As String = "Paginas/img/ImgLoad/Eventos/Evento" & ResulGrabar.ToString.Trim
                Dim NombreImagenActual As String = Me.FileUpload1.FileName
                NombreImagenActual = NombreImagenActual.Split("\")(NombreImagenActual.Split("\").Length - 1)
                NombreImagenActual = NombreImagenActual.Split(".")(NombreImagenActual.Split(".").Length - 1)
                nombreImagen = nombreImagen & "." & NombreImagenActual
                Me.FileUpload1.SaveAs(Server.MapPath("~") & "/" & nombreImagen)
                NombreImagenActual = nombreImagen.Split("/")(nombreImagen.Split("/").Length - 1)
                objEvento.ImagenMailEvento = NombreImagenActual
                objEvento.Codigo = ResulGrabar
                ResulGrabar = objNeg.UpdatearImagenEvento(objEvento)
            Else
                ResulGrabar = objNeg.UpdatearEvento(objEvento)
            End If

            If ResulGrabar > 0 Then
                UtilidadesPagina.mensaje("Evento guardado correctamente", Me.Page)
                Me.btnAgregarFiltro.Visible = True
                Me.hdnIdEvento.Value = ResulGrabar
            End If
        Catch ex As Exception
        Finally
            objNeg.Dispose()
            objEvento.Dispose()
            objNeg = Nothing
            objEvento = Nothing
        End Try
    End Sub

    Protected Sub btnVolver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVolver.Click
        Response.Redirect("ListarEventos.aspx?PostBack=SI")
    End Sub

    Protected Sub btnAgregarFiltro_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregarFiltro.Click
        Response.Redirect("MantencionFiltros.aspx?EventoID=" & Me.hdnIdEvento.Value.Trim)
        Response.End()
    End Sub
End Class
